package com.example.dukesbank.web.template;

import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

public class DefinitionTag extends SimpleTagSupport {
	private HashMap<String, ArrayList<Parameter>> screens = null;
	private String definitionName = null;
	private String screenId;

	public DefinitionTag() {
		super();
	}

	public HashMap<String, ArrayList<Parameter>> getScreens() {
		return screens;
	}

	public void setName(String name) {
		this.definitionName = name;
	}

	public void setScreen(String screenId) {
		this.screenId = screenId;
	}

	@Override
	public void doTag() {
		try {
			screens = new HashMap<String, ArrayList<Parameter>>();

			getJspBody().invoke(null);

			Definition definition = new Definition();
			PageContext context = (PageContext) getJspContext();
			ArrayList<Parameter> params = screens.get(screenId);
			for (Parameter nr : params)
				System.out.println(nr.getName());

			Set<String> screenset = screens.keySet();
			Object[] anArray;
			anArray = new Object[screenset.size()];
			anArray = screenset.toArray();

			for (int i = 0; i < anArray.length; i++) {
				System.out.print(anArray[i]);
			}

			for (Parameter ir : params) {
				definition.setParam(ir);
			}

			// put the definition in the page context
			context.setAttribute(definitionName, definition,
					PageContext.APPLICATION_SCOPE);

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
